package com.hznu.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {

    @ApiModelProperty(value = "是否成功")
    private boolean success;    //是否成功

    @ApiModelProperty(value = "返回码")
    private Integer code;   //返回码

    @ApiModelProperty(value = "消息提示")
    private String message; //消息提示

    @ApiModelProperty(value = "返回对象")
    private Map<String ,Object> data = new HashMap<>(); //返回对象

    //把构造方法私有
    private R(){}

    //成功静态方法
    public static R ok(){
        R result = new R();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("success");
        return result;
    }

    //失败静态方法
    public static R error(){
        R result = new R();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMessage("false");
        return result;
    }

    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
